const {
  override,
  fixBabelImports,
  addLessLoader,
  addWebpackPlugin,
  addWebpackAlias,
  addDecoratorsLegacy,
  useBabelRc,
} = require('customize-cra')
const webpack = require('webpack')
const webpackMerge = require('webpack-merge')
const theme = require('./theme.ts')
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin')

const path = require('path')
const resolvePath = (dir) => path.join(__dirname, '.', dir)
// 打包分析
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// 修改 HOST PORT
// process.env.HOST = '10.98.214.92:7000';
process.env.PORT = 7777
// 生成环境是否打包 Source Map
process.env.GENERATE_SOURCEMAP = false

console.log(`********** 自定义构建环境：${process.env.BUILD_ENV} **********`)
// 开发环境
// const isEnvDevelopment = process.env.NODE_ENV === 'development';
// 生产环境
// const isEnvProduction = process.env.NODE_ENV === 'production';
module.exports = {
  webpack: (config, env) => {
    config = override(
      // 按需加载antd
      fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: true,
      }),
      // less
      addLessLoader({
        modifyVars: theme, // 主题
        javascriptEnabled: true,
        modules: true, // 样式modules
        localIdentName: '[name]__[local]__[hash:base64:5]',
      }),
      // 替换
      addWebpackPlugin(new AntdDayjsWebpackPlugin()),
      // 配置路径别名
      addWebpackAlias({
        '@': resolvePath('src'),
        '@routes': resolvePath('src/routes'),
        '@pages': resolvePath('src/pages'),
        '@components': resolvePath('src/components'),
        '@styles': resolvePath('src/styles'),
        '@api': resolvePath('src/api'),
        '@utils': resolvePath('src/utils'),
        '@config': resolvePath('src/config'),
        '@reducers': resolvePath('src/redux/reducers'),
        '@effects': resolvePath('src/redux/effects'),
      }),
      // 对Decorators支持
      addDecoratorsLegacy(),
      // eslint-disable-next-line react-hooks/rules-of-hooks
      useBabelRc(),
    )(config, env)

    return webpackMerge(config, {
      plugins: [
        // 定义全局变量
        new webpack.DefinePlugin({
          'process.env.BUILD_ENV': JSON.stringify(process.env.BUILD_ENV),
        }),
        // new BundleAnalyzerPlugin(),
      ],
    })
  },
}
